package org.pim.smart.utils;

import java.util.Enumeration;
import java.util.ResourceBundle;
import java.util.Map;
import java.util.TreeMap;

public abstract class AbstractProperties {
	private String name;
    private String propFile;
    private ResourceBundle resourceBundle;
    
    protected AbstractProperties(String name, String file)
    {
        resourceBundle = null;
        this.name = name;
        propFile = file;
        resourceBundle = ResourceBundle.getBundle(propFile);
    }

    public String getProperty(String key)
    {
        return resourceBundle.getString(key);
    }
    
    public AbstractProperties getProperties()
    {
        AbstractProperties ap = PropertiesCache.getINSTANCE().getProperties(name);
        if(ap == null)
        	PropertiesCache.getINSTANCE().addProperties(name, this);
        return ap;
    }

    public Map<String, String> getProperties(String startWith)
    {
        Map<String, String> sm = new TreeMap<String, String>();
        Enumeration<String> en = resourceBundle.getKeys();
        do
        {
            if(!en.hasMoreElements())
                break;
            String key = en.nextElement();
            if(key != null && key.startsWith(startWith))
                sm.put(key, getProperty(key));
        } while(true);
        return sm;
    }
}
